﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.AspNetCore.Mvc.Extensions;
using FwProject.Comm.ExcelUtil;
using FwProject.Controllers;
using FwProject.Core.TypeNumber;
using FwProject.FactoryInfo;
using FwProject.TypeNumber;
using FwProject.TypeNumber.Dto;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FwProject.Web.Controllers
{
    [AbpMvcAuthorize]
    public class TypeNumberController: FwProjectControllerBase
    {
        private readonly ITypeNumberInfoAppService _typeNumberInfoAppService;

        private readonly IFactoryNameInfoAppService _factoryNameInfoAppService;
        private readonly IHostingEnvironment _env;

        public TypeNumberController(ITypeNumberInfoAppService typeNumberInfoAppService, IFactoryNameInfoAppService factoryNameInfoAppService,
            IHostingEnvironment env)
        {
            _typeNumberInfoAppService = typeNumberInfoAppService;
            _factoryNameInfoAppService = factoryNameInfoAppService;
            _env = env;
        }
        /// <summary>
        /// 获取型号列表
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public async Task<ActionResult> Index(int pageIdx,int size,DateTime? beginTime,DateTime? endTime,string factoryid="")
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;

            var typeNumber = await _typeNumberInfoAppService.GetPagedListAsync(pageIdx,size,beginTime,endTime,factoryid);
            if (Request.IsAjaxRequest())
            {
                return PartialView("IndexPartial", typeNumber);
            }
            return View(typeNumber);
        }
        /// <summary>
        /// 获取该工厂下的所有型号
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <returns></returns>
        public async Task<ActionResult> MoreTypeNumber(int pageIdx)
        {
            if (pageIdx == 0)
                pageIdx = 1;

            var user =await GetCurrentUserAsync();
                var typeNumber = await _typeNumberInfoAppService.GetPagedListAsync(pageIdx, 20, null, null, user.FactoryinfoId.ToString());
                return PartialView("MoreTypeNumber", typeNumber);
        }

        public ActionResult GetTypeNumbersView(List<TypeNumberInfoDto> typeNumberInfoDtos, Guid id, int trlength) {

               ViewBag.RestaurantId = id;
                ViewBag.trlength = trlength;
            return PartialView("GetTypeNumbersView", typeNumberInfoDtos);
        }

        /// <summary>
        /// 创建型号
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> Create()
        {
            var factory = await _factoryNameInfoAppService.GetAllAsync("");
            ViewBag.Factory = factory.Select(c => new SelectListItem() { Value = c.Id.ToString(), Text = c.FactoryName });
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> CreateTypeNumber(TypeNumberInfoInput input)
        {
            var model = await _typeNumberInfoAppService.Create(input);
            return Json(model);
        }

        /// <summary>
        /// 修改型号信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> Edit(Guid id)
        {
            var model = await _typeNumberInfoAppService.GetByIdAsync(id);
            return View(model);
        }
        [HttpPost]
        public async Task<JsonResult> EditTypeNumber(Guid id, TypeNumberInfoInput input)
        {
            await _typeNumberInfoAppService.Update(id,input);
            return Json(id);
        }

        /// <summary>
        /// 删除型号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JsonResult> DeleteTypeNumber(Guid id)
        {
            await _typeNumberInfoAppService.Delete(id);
            return Json(id);
        }
        /// <summary>
        /// Excel导出
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> ExportExcel()
        {
            var list = await _typeNumberInfoAppService.GetAllTypeNumber();
            return DoExcel<TypeNumberInfoDto>(list, _env);
        }
    }
}
